package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gxuwz.medical.domain.menu.Menu;
import com.gxuwz.medical.domain.role.Role;
import com.gxuwz.medical.domain.user.User;
import com.gxuwz.medical.vo.PageBean;

/**
 * 角色管理控制模块
 * 
 * @author 演示
 * 
 */
public class RoleServlet extends BaseServlet {
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String url = request.getRequestURI();
		String methodName = url.substring(url.lastIndexOf("/") + 1,
				url.lastIndexOf("."));
		Method method = null;
		try {
			method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public void jump(HttpServletRequest request, HttpServletResponse response,String path) throws ServletException, IOException, SQLException{
		Role r = new Role();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = r.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path="page/role/list.jsp";
		Role r = new Role();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = r.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}

	public void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "page/role/list.jsp";
		String roleid = request.getParameter("roleid");
		roleid = new String(roleid.getBytes("ISO-8859-1"),"UTF-8");
		System.out.println(roleid);
		Role r = new Role();
		r.delete(roleid);
		jump(request,response,path);
	}

	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "list.jsp";
		String roleid = request.getParameter("roleid");
		String roleName = request.getParameter("roleName");
		JSONArray menuid = JSON.parseArray(request.getParameter("id"));
		//添加角色
		Role r = new Role();
		r.setRoleid(roleid);
		r.setRoleName(roleName);
		for(int i=0;i<menuid.size();i++){
			r.addMenu((String)menuid.get(i));
		}
		r.add();
		jump(request,response,path);
	}

	public void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "list.jsp";
		String roleid = request.getParameter("roleid");
		String roleName = request.getParameter("roleName");
		JSONArray menuid = JSON.parseArray(request.getParameter("id"));
		System.out.println(menuid);
		Role r = new Role();
		r.setRoleid(roleid);
		r.setRoleName(roleName);
		for(int i=0;i<menuid.size();i++){
			r.addMenu((String)menuid.get(i));
		}
		r.update();
		jump(request,response,path);
	}

}
